package com.bbu.javaexam.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bbu.javaexam.domain.RespData;
import com.bbu.javaexam.domain.Score;
import com.bbu.javaexam.service.ScoreService;
import com.bbu.javaexam.util.RespDataHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Alex
 * @version 1.0.0
 * @ClassName ScoreController.java
 * @Description TODO
 */
@RestController
public class ScoreController {
    @Autowired
    private ScoreService scoreService;

    @GetMapping("/scores")
    public RespData findAll() {
        List<Score> res = scoreService.findAll();
        return RespDataHandler.buildRespData(200, "查询所有学生成绩", res);
    }

    @GetMapping("/score/{page}/{size}/{studentId}")
    public RespData findById(@PathVariable("page") Integer page, @PathVariable("size") Integer size, @PathVariable("studentId") Integer studentId) {
        Page<Score> scorePage = new Page<>(page, size);
        IPage<Score> res = scoreService.findById(scorePage, studentId);
        return RespDataHandler.buildRespData(200, "根据ID查询成绩", res);
    }

    @GetMapping("/score/{studentId}")
    public RespData findById(@PathVariable("studentId") Integer studentId) {
        List<Score> res = scoreService.findById(studentId);
        if (!res.isEmpty()) {
            return RespDataHandler.buildRespData(200, "根据ID查询成绩", res);
        } else {
            return RespDataHandler.buildRespData(404, "ID不存在", res);
        }
    }

    @PostMapping("/score")
    public RespData add(@RequestBody Score score) {
        int res = scoreService.add(score);
        if (res == 0) {
            return RespDataHandler.buildRespData(400, "成绩添加失败", res);
        } else {
            return RespDataHandler.buildRespData(200, "成绩添加成功", res);
        }
    }

    @GetMapping("/scores/{examCode}")
    public RespData findByExamCode(@PathVariable("examCode") Integer examCode) {
        List<Score> scores = scoreService.findByExamCode(examCode);
        return RespDataHandler.buildRespData(200, "查询成功", scores);
    }


}
